package com.xxl.sso.server.service.impl;

import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.common.SysConstant;
import com.alibaba.algo.cooperation.feign.service.DictFeignService;
import com.alibaba.algo.cooperation.feign.vo.res.SysDictResponseVO;
import com.xxl.sso.server.core.model.DictInfo;
import com.xxl.sso.server.core.result.ReturnT;
import com.xxl.sso.server.service.DictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DictServiceImpl implements DictService {

    @Resource
    private DictFeignService dictFeignService;

    @Override
    public ReturnT<List<DictInfo>> selectPageDict() {
        BaseResult<List<SysDictResponseVO>> result = dictFeignService.selectPageDict();
        if (!result.getCode().equals(SysConstant.OK)) {
            log.error("selectPageDict failed,code:{},message:{}", result.getCode(), result.getMessage());
            return new ReturnT(ReturnT.FAIL_CODE, result.getMessage());
        }
        List<SysDictResponseVO> list = result.getData();
        List<DictInfo> returnList = new ArrayList<>();
        if (list != null) {
            for (SysDictResponseVO temp : list) {
                DictInfo dictInfo = new DictInfo();
                dictInfo.setCode(temp.getCode());
                dictInfo.setDesc(temp.getDesc());
                dictInfo.setId(temp.getId());
                dictInfo.setName(temp.getName());
                dictInfo.setParentCode(temp.getParentCode());
                dictInfo.setSort(temp.getSort());
                returnList.add(dictInfo);
            }
        }
        return new ReturnT(returnList);
    }

}
